package com.youzhi.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youzhi.backend.dto.Result;
import com.youzhi.backend.request.*;
import com.youzhi.backend.service.IWorkflowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.crypto.Data;

@RestController
@Api(tags = "工作流")
@RequestMapping("/workflow")
public class WorkflowController extends BaseController {


    @Autowired
    private IWorkflowService workflowService;

    @ApiOperation(value = "分页查询工作流部署")
    @RequiresUser
    @PostMapping("/processes/deployment/page")
    public Result processesDeploymentPage(@RequestBody ProcessesDeploymentPageRequest request) {
        IPage deploymentIPage = workflowService.processesDeploymentPage(request);
        return Result.ok(deploymentIPage);
    }

    @ApiOperation(value = "删除工作流部署")
    @RequiresUser
    @PostMapping("/processes/deployment/delete")
    public Result deleteDeployment(@RequestBody DeleteDeploymentRequest request) {
        verifyIfBlank(request, DeleteDeploymentRequest.Fields.deploymentId,
                DeleteDeploymentRequest.Fields.cascade);
        workflowService.deleteDeployment(request);
        return Result.ok();
    }


    @ApiOperation(value = "分页查询工作流定义")
    @RequiresUser
    @PostMapping("/processes/definition/page")
    public Result processesDefinitionPage(@RequestBody ProcessesDefinitionPageRequest request) {
        IPage page = workflowService.processesDefinitionPage(request);
        return Result.ok(page);
    }

    @ApiOperation(value = "分页查询正在运行工作流实例")
    @RequiresUser
    @PostMapping("/processes/instance/page")
    public Result processesInstancePage(@RequestBody ProcessesInstancePageRequest request) {
        IPage page = workflowService.processesInstancePage(request);
        return Result.ok(page);
    }


    @ApiOperation(value = "分页查询已结束的工作流实例")
    @RequiresUser
    @PostMapping("/processes/history/instance/page")
    public Result historyProcessesInstancePage(@RequestBody HistoryProcessesInstancePageRequest request) {
        IPage page = workflowService.historyProcessesInstancePage(request);
        return Result.ok(page);
    }


    @ApiOperation(value = "开启工作流实例")
    @RequiresPermissions("processDefinition:start")
    @PostMapping("/start/process")
    public Result startProcessInstance(@RequestBody StartProcessInstanceRequest request) {
        verifyIfBlank(request, StartProcessInstanceRequest.Fields.processDefinitionKey);
        workflowService.startProcessInstance(request.getProcessDefinitionKey());
        return Result.ok();
    }


    @ApiOperation(value = "删除工作流实例")
    @RequiresPermissions("processInstance:delete")
    @PostMapping("/delete/process")
    public Result deleteProcessInstance(@RequestBody DeleteProcessInstanceRequest request) {
        verifyIfBlank(request, DeleteProcessInstanceRequest.Fields.processInstanceId);
        workflowService.deleteProcessInstance(request);
        return Result.ok();
    }

    @ApiOperation(value = "删除历史工作流")
    @RequiresPermissions("historyProcessInstance:delete")
    @PostMapping("/delete/history/process")
    public Result deleteHistoryProcessInstance(@RequestBody DeleteProcessInstanceRequest request){
        verifyIfBlank(request,DeleteProcessInstanceRequest.Fields.processInstanceId);
        workflowService.deleteHistoryProcessInstance(request.getProcessInstanceId());
        return Result.ok();
    }


    @ApiOperation(value = "暂停工作流实例")
    @RequiresPermissions("processInstance:suspend")
    @PostMapping("/suspend/process")
    public Result suspendProcessInstance(@RequestBody SuspendProcessInstanceRequest request) {
        verifyIfBlank(request, SuspendProcessInstanceRequest.Fields.isSuspend,
                SuspendProcessInstanceRequest.Fields.processInstanceId);
        workflowService.suspendProcessInstance(request);
        return Result.ok();
    }


}
